package com.syni.mdd.sjb.common.component.utils.common.utils;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @className MapUtil
 * @description TOO
 * @Author cfx
 * @DATE 2020/4/28 11:05
 * @VERSION 1.0
 **/
public class MapUtil extends MapUtils {
    public static void removeNullValues(List<Map<String,Object>> list){
        if(CollectionUtils.isNotEmpty(list)){
            list.forEach(obj -> {
                removeNullValues(obj);
            });
        }
    }

    public static void removeNullValues(Map<String,Object> obj){
        Set<String> removeKeys = new HashSet<>();
        if(obj != null && obj.size() > 0){
            Set<String> keys = obj.keySet();
            for(String key : keys) {
                Object value = obj.get(key);
                if (value instanceof Map) {
                    removeNullValues((Map) value);
                }
                if (value == null) {
                    removeKeys.add(key);
                }
            }
            if(removeKeys.size() > 0){
                removeKeys.forEach(key -> {
                    obj.remove(key);
                });
            }
        }
    }

    public static boolean checkKeyExist(Map<String, Object> content, String keyName) {
        return (content.containsKey(keyName) && StringUtils.checkNullAndEmpty(content.get(keyName)));
    }

    public static void copyStringData(Map<String,Object> originMap,Map<String,String> targetMap){
        if(originMap.size() > 0){
            for(Map.Entry<String,Object> entry : originMap.entrySet()){
                targetMap.put(entry.getKey(),entry.getValue().toString());
            }
        }
    }

}
